package cn.kgc.springcloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springcloud.entity.Account;
import cn.kgc.springcloud.service.AccountService;
import cn.kgc.springcloud.mapper.AccountMapper;
import org.springframework.stereotype.Service;

/**
* @author mengshun
* @description 针对表【account_tbl】的数据库操作Service实现
* @createDate 2025-01-16 09:00:51
*/
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>implements AccountService{


    @Override
    public void debit(String userId, int money) {

        // 判定账务余额是否充足
        LambdaQueryWrapper<Account> lambda = new QueryWrapper<Account>().lambda();
        lambda.eq(Account::getUserId,userId);

        Integer DBMoney = baseMapper.selectOne(lambda).getMoney();

        if (money> DBMoney){
            throw  new RuntimeException("余额不足");
        }

        LambdaUpdateWrapper<Account> lambda1 = new UpdateWrapper<Account>().lambda();
        lambda1.setSql("money = money - "+money)
                .eq(Account::getUserId,userId);

        baseMapper.update(lambda1);

    }
}




